<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从套接字中读取最大长度的数据</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.socket-listen.html">« socket_listen</a></li>
      <li style="float: right;"><a href="function.socket-recv.html">socket_recv »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.sockets.html">Socket 函数</a></li>
    <li>从套接字中读取最大长度的数据</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.socket-read" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_read</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">socket_read</span> &mdash; <span class="dc-title">从套接字中读取最大长度的数据</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-read-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_read</strong></span>(<span class="methodparam"><span class="type"><a href="class.socket.html" class="type Socket">Socket</a></span> <code class="parameter">$socket</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = <strong><code>PHP_BINARY_READ</code></strong></span></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>socket_read()</strong></span> 函数是从 <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span>
   或 <span class="function"><a href="function.socket-accept.html" class="function">socket_accept()</a></span> 函数所创建的 <span class="classname"><a href="class.socket.html" class="classname">Socket</a></span>
   实例 <code class="parameter">socket</code> 中读取数据。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-read-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">socket</code></dt>

     <dd>

      <p class="para">
       使用 <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span>
       或 <span class="function"><a href="function.socket-accept.html" class="function">socket_accept()</a></span>
       函数创建的 <span class="classname"><a href="class.socket.html" class="classname">Socket</a></span> 实例。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">length</code> 参数指定了最大能够读取的字节数。否则您可以使用
       <strong><code>\r</code></strong>、<strong><code>\n</code></strong>、<strong><code>\0</code></strong>
       结束读取（根据 <code class="parameter">mode</code> 参数设置，请参见下文）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">mode</code></dt>

     <dd>

      <p class="para">
       可选参数 <code class="parameter">mode</code> 是一个命名常量：
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <strong><code>PHP_BINARY_READ</code></strong> （默认）- 使用系统的
          <code class="literal">recv()</code> 函数。二进制安全地读取数据。
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>PHP_NORMAL_READ</code></strong> - 读取到
          <code class="literal">\n</code>、<code class="literal">\r</code> 时停止。
         </span>
        </li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-read-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>socket_read()</strong></span> 返回一个字符串，表示接收到的数据。如果发生了错误（包括远程主机关闭了连接），则返回 <strong><code>false</code></strong>。错误码可使用
   <span class="function"><a href="function.socket-last-error.html" class="function">socket_last_error()</a></span> 获取。也可使用 <span class="function"><a href="function.socket-strerror.html" class="function">socket_strerror()</a></span>
   来获取错误码的文字描述。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    当套接字中没有更多数据可读取时，<span class="function"><strong>socket_read()</strong></span>
    返回一个长度为 0 的空字符串（&quot;&quot;）。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.socket-read-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
  <td>8.0.0</td>
  <td>
   现在 <code class="parameter">socket</code> 是 <span class="classname"><a href="class.socket.html" class="classname">Socket</a></span> 实例，
   之前是 <span class="type">resource</span>。
  </td>
 </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-read-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.socket-accept.html" class="function" rel="rdfs-seeAlso">socket_accept()</a> - 接受套接字上的连接</span></li>
    <li class="member"><span class="function"><a href="function.socket-bind.html" class="function" rel="rdfs-seeAlso">socket_bind()</a> - 给套接字绑定名字</span></li>
    <li class="member"><span class="function"><a href="function.socket-connect.html" class="function" rel="rdfs-seeAlso">socket_connect()</a> - 开启一个套接字连接</span></li>
    <li class="member"><span class="function"><a href="function.socket-listen.html" class="function" rel="rdfs-seeAlso">socket_listen()</a> - 监听套接字的连接</span></li>
    <li class="member"><span class="function"><a href="function.socket-last-error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - 返回套接字上的最后一个错误</span></li>
    <li class="member"><span class="function"><a href="function.socket-strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - 返回描述套接字错误的字符串</span></li>
    <li class="member"><span class="function"><a href="function.socket-write.html" class="function" rel="rdfs-seeAlso">socket_write()</a> - 向套接字写数据</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>